package com.openlogic.logic.service.datasource.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.openlogic.common.core.constants.CacheConstant;
import com.openlogic.common.core.exception.logic.RuleException;
import com.openlogic.common.redis.service.RedisService;
import com.openlogic.datasource.api.entity.DataColumnEntity;
import com.openlogic.datasource.api.entity.DataSourceEntity;
import com.openlogic.datasource.api.entity.table.Tables;
import com.openlogic.datasource.api.enums.DataCategory;
import com.openlogic.datasource.api.query.DataColumnQuery;
import com.openlogic.datasource.api.vo.DataColumnVo;
import com.openlogic.logic.mapper.datasource.DataColumnMapper;
import com.openlogic.logic.service.datasource.DataColumnService;

import cn.hutool.core.util.StrUtil;

/**
* @ClassName: DataColumnServiceImpl
* @Description: 元数据信息表 ServiceImpl业务实现层
* @author weiyongpeng
* @date 2024年11月22日 上午9:08:54
 */
@Component
public class DataColumnServiceImpl extends ServiceImpl<DataColumnMapper, DataColumnEntity> implements DataColumnService {

	@Inject
	private DataColumnMapper dataColumnMapper;
	
	@Inject
	private RedisService redisService;
	
	@Override
	public DataColumnVo getMetadataColumnById(String id, String tableId, String columnId) {
		DataColumnVo dataColumnEntity = dataColumnMapper.findMetadataColumnById(id, tableId, columnId);
        return dataColumnEntity;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<DataColumnEntity> getDataMetadataColumnList(DataColumnQuery dataColumnQuery) {
		if (StrUtil.isBlank(dataColumnQuery.getSourceId())) {
			throw new RuleException("数据源不能为空");
		}
		List<DataSourceEntity> sourceList = redisService.getCacheList(CacheConstant.METADATA_SOURCE_KEY);
		Optional<DataSourceEntity> dsource = sourceList.stream().filter(source -> source.getId().equals(dataColumnQuery.getSourceId())).findFirst();
		if (!dsource.isPresent()) {
			throw new RuleException("数据源不能为空");
		}
		String category = dsource.get().getCategory();
//		List<DataColumnEntity> columnList = (List<DataColumnEntity>) redisService
//				.getCacheMapValue(CacheConstant.METADATA_COLUMN_KEY, dataColumnQuery.getTableId());
		List<DataColumnEntity> columnList  = (List<DataColumnEntity>) redisService.getCacheMapValue(CacheConstant.METADATA_COLUMN_KEY, dataColumnQuery.getTableId(), DataColumnEntity.class);
		Stream<DataColumnEntity> stream = Optional.ofNullable(columnList).orElseGet(ArrayList::new).stream();
		List<DataColumnEntity> list = new ArrayList<>();
		if (category.equals(String.valueOf(DataCategory.RDB.getLevel()))) {
			list = stream.sorted((x,y) -> x.getColumnPosition().compareTo(y.getColumnPosition())).collect(Collectors.toList());
		}else if(category.equals(String.valueOf(DataCategory.EXCEL.getLevel()))) {
			list = stream.collect(Collectors.toList());
		}
		return CollectionUtils.isEmpty(list)
				? StringUtils.isNotBlank(dataColumnQuery.getColumnName())
//						? dataColumnMapper.selectList(new LambdaQueryWrapper<DataColumnEntity>()
//								.eq(DataColumnEntity::getSourceId, dataColumnQuery.getSourceId())
//								.eq(DataColumnEntity::getTableId, dataColumnQuery.getTableId())
//								.eq(DataColumnEntity::getColumnName, dataColumnQuery.getColumnName())
//								.orderByAsc(DataColumnEntity::getColumnPosition))
						? dataColumnMapper.selectListByQuery(QueryWrapper.create().select().from(DataColumnEntity.class)
								.where(Tables.dataColumn.sourceId.eq(dataColumnQuery.getSourceId()))
								.and(Tables.dataColumn.tableId.eq(dataColumnQuery.getTableId()))
								.and(Tables.dataColumn.columnName.eq(dataColumnQuery.getColumnName()))
								.orderBy(DataColumnEntity::getColumnPosition).asc())
						: dataColumnMapper.selectListByQuery(QueryWrapper.create().select().from(DataColumnEntity.class)
								.where(Tables.dataColumn.sourceId.eq(dataColumnQuery.getSourceId()))
								.and(Tables.dataColumn.tableId.eq(dataColumnQuery.getTableId()))
								.orderBy(DataColumnEntity::getColumnPosition).asc())
				: StringUtils.isNotBlank(dataColumnQuery.getColumnName())
						? list.stream()
								.sorted((x,y) -> x.getColumnPosition().compareTo(y.getColumnPosition()))
								.filter(column -> column.getColumnName().equals(dataColumnQuery.getColumnName()))
								.collect(Collectors.toList())
						: list;
	}

}
